import java.awt.*;
import java.util.*;
import javax.swing.*;

public class Example2 extends JFrame {

   Container  c 	= null;
   JPanel     pn 	= new JPanel( );
   Graphics   g		= null;
   public Example2 () {
	c = getContentPane();
	pn.setBackground( Color.black ); 

	c.add( pn, BorderLayout.CENTER );
	setSize(600, 400);
	setVisible( true );
	
	g = pn.getGraphics();
	// g.drawString("Hello ...", 20, 50);
   }

   public static void main( String args[] ) {

	Example2 app = new Example2();

	for ( int i = 1; i <= 4; i ++ )
		( new Thread ( new Drawer( i, i * 100, i * 100, app.pn )) ).start();
   }
}

class Drawer implements Runnable {
   static   Random rdm = new Random( ) ;
   int myID, x, y;
   JPanel   pn = null;
   Graphics g;
   public Drawer( int id, int x, int y, JPanel p ) { 
	myID = id ; this.x = x; this.y = y; pn = p; g = pn.getGraphics();
        g.setXORMode( Color.blue );
   }

   public void run() {
	int width = 10, height = 10;
        int inc = 4;
	int limit = 100;
	g.drawOval(x, y, width,height); 
	while ( true ) {
	    // try { Thread.sleep( rdm.nextInt(2000-500 + 1) + 500 ) ; } catch (InterruptedException e) { } 
	    // g.setColor( pn.getBackground() );
	    // g.fillRect( x, y-25, 400, 28);
	    // g.setColor( Color.blue );
	    // g.drawString("Drawer " + myID + " has drawed " + k + " times.", x, y);

	    g.drawOval(x, y, width, height); 
	    width += inc; height += inc;
            if ( width < 0 || width > limit ) inc = -1 * inc;
	  ;
	    pn.invalidate();
	}
   }
}
